package org.redr.canvas;

import org.redr.widgetGUI.settings.CoreSettings;

import com.trolltech.qt.gui.QCheckBox;
import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QFileDialog;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QListWidget;
import com.trolltech.qt.gui.QListWidgetItem;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QMessageBox.StandardButton;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QTabWidget;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;
	
public class CanvasOptionsDlg extends QDialog {
	private CoreSettings settings; 
	private QTabWidget tabs;
	private QLineEdit emailEdit;
	private QCheckBox checkForUpdates;
	private QCheckBox checkForPackageUpdates;
	private QCheckBox askBeforeClosing;
	private QCheckBox askBeforeWidgetDelete;
	private QCheckBox snapToGridCB;
	private QCheckBox showSignalNamesCB;
	private QComboBox schemaIconSizeCombo;
	private QComboBox toolbarIconSizeCombo;
	private QComboBox windowStyleSelect;
	private QComboBox language;
	private QComboBox verbosityCombo;
	private QCheckBox displayTraceback;
	private QCheckBox focusOnCatchExceptionCB;
	private QCheckBox uploadError;
	private QCheckBox askToUploadError;
	private QCheckBox writeLogFileCB;
	private QListWidget libListBox;
	private QListWidget templatesDirList;
	
	public CanvasOptionsDlg(){
		super();
		settings = RedRApplication.coreSettings();
		setWindowTitle("Canvas Options");
		resize(450, 300);
		tabs = new QTabWidget(this);
		
		QWidget generalBox = new QWidget(this);
		tabs.addTab(generalBox, "General");
		generalBox.setLayout(new QVBoxLayout());
		// TODO generalBox.layout().setAlignment(new QtAlignment());
		
		QWidget underHood = new QWidget(this);
		tabs.addTab(underHood, "Under The Hood");
		underHood.setLayout(new QVBoxLayout());
		
		QWidget exceptions = new QWidget(this);
		tabs.addTab(exceptions, "Exceptions");
		exceptions.setLayout(new QVBoxLayout());
		
		QWidget rSettings = new QWidget(this);
		tabs.addTab(rSettings, "R Settings");
		rSettings.setLayout(new QVBoxLayout());
		
		tabs.currentChanged.connect(this, "onTabChanged(int)");
		
		// General Tab
		// email section
		QWidget emailBox = new QWidget(generalBox);
		QLabel emailLabel = new QLabel(emailBox);
		emailLabel.setText("email");
		emailEdit = new QLineEdit(emailBox);
		
		// check for updates
		QWidget checkForUpdatesBox = new QWidget(generalBox);
		QLabel checkForUpdatesLabel	=	new QLabel(checkForUpdatesBox);
		checkForUpdatesLabel.setText("Check For Updates");
		checkForUpdates = new QCheckBox(checkForUpdatesBox);
		if (Boolean.parseBoolean(settings.getSetting("checkForUpdates")))
			checkForUpdates.setChecked(true);
		
		// check for package updates
		QWidget checkForPackageUpdatesBox = new QWidget(generalBox);
		QLabel checkForPackageUpdatesLabel	=	new QLabel(checkForPackageUpdatesBox);
		checkForPackageUpdatesLabel.setText("Check For Package Updates");
		checkForPackageUpdates = new QCheckBox(checkForPackageUpdatesBox);
		if (Boolean.parseBoolean(settings.getSetting("checkForPackageUpdates")))
			checkForPackageUpdates.setChecked(true);
		
		// ask before closing
		QWidget askBeforeClosingBox = new QWidget(generalBox);
		QLabel askBeforeClosingLabel	=	new QLabel(askBeforeClosingBox);
		askBeforeClosingLabel.setText("Ask Before Closing");
		askBeforeClosing = new QCheckBox(askBeforeClosingBox);
		if (Boolean.parseBoolean(settings.getSetting("askBeforeClosing")))
			askBeforeClosing.setChecked(true);
		// ask before closing
		QWidget askBeforeWidgetDeleteBox = new QWidget(generalBox);
		QLabel askBeforeWidgetDeleteLabel	=	new QLabel(askBeforeWidgetDeleteBox);
		askBeforeWidgetDeleteLabel.setText("Ask Before Widget Delete");
		askBeforeWidgetDelete = new QCheckBox(askBeforeWidgetDeleteBox);
		if (Boolean.parseBoolean(settings.getSetting("askBeforeWidgetDelete")))
			askBeforeWidgetDelete.setChecked(true);
		
		// look and feel
		QWidget lookFeelBox = new QWidget(generalBox);
		//QLabel lookFeelLabel
		
		snapToGridCB = makeCheckBox(lookFeelBox, "Snap Widgets To Grid");
		showSignalNamesCB = makeCheckBox(lookFeelBox, "Show Signal Names Between Widgets");
		schemaIconSizeCombo = makeComboBox(lookFeelBox, "Save Size And Position of Widgets");
		toolbarIconSizeCombo	=	makeComboBox(lookFeelBox, "Widget Tree Icon Size");
		windowStyleSelect		= 	makeComboBox(lookFeelBox, "Window Style");
		language				=	makeComboBox(lookFeelBox, "Language");
		templatesDirList 		= 	makeListBox(underHood, "Templates Directory");
		QWidget tempManipBox	=	new QWidget();
		tempManipBox.setLayout(new QHBoxLayout());
		underHood.layout().addWidget(tempManipBox);
		QPushButton addTempDir	=	new QPushButton(underHood);
		underHood.layout().addWidget(addTempDir);
		addTempDir.clicked.connect(this, "addTemplateDirectory()");
		QPushButton removeTempDir	=	new QPushButton(underHood);
		underHood.layout().addWidget(removeTempDir);
		removeTempDir.clicked.connect(this, "removeTemplateDirectory()");
		// TODO work on adding some functions for testing here
		
		// Exceptions
		QWidget debug = new QWidget(exceptions);
		
		verbosityCombo 		= 	makeComboBox(debug, "Set Level Of Widget Output");
		displayTraceback 	=	makeCheckBox(debug, "Display Traceback");
		focusOnCatchExceptionCB	=	makeCheckBox(debug, "Show Widget On Exception");
		uploadError			=	makeCheckBox(debug, "Submit Error On Exception");
		askToUploadError	=	makeCheckBox(debug, "Always Ask Before Uploading");
		writeLogFileCB		=	makeCheckBox(debug, "Save content of Output to log file");
		QLabel fileDirLocation	=	new QLabel(debug);
		debug.layout().addWidget(fileDirLocation);
		fileDirLocation.setText("Current Log File: " + settings.getSetting("logsDir"));
		QPushButton browseButton = new QPushButton(debug);
		browseButton.clicked.connect(this, "browseLogFile()");
		// TODO set number of days to keep log file.
		
		// R Settings
		QWidget rlibrariesBox = new QWidget(rSettings);
		QLabel libInfo		=	new QLabel(rlibrariesBox);
		libInfo.setText("Repository URL: " + settings.getSetting("repos"));
		libListBox 			= 	new QListWidget();
		rlibrariesBox.layout().addWidget(libListBox);
		// libListBox.currentItemChanged.connect(this, "setMirror(QListWidgetItem, QListWidgetItem)");
	
		QWidget globalButtonsBox = new QWidget();
		this.layout().addWidget(globalButtonsBox);
		globalButtonsBox.setLayout(new QHBoxLayout());
		
		QPushButton okButton = new QPushButton(globalButtonsBox);
		okButton.clicked.connect(this, "accept()");
		
		QPushButton cancelButton = new QPushButton(globalButtonsBox);
		cancelButton.clicked.connect(this, "reject()");
		
	}
	
	
	@Override
	public void reject(){
		
	}
	
	@Override
	public void accept(){
		settings.setSetting("email", this.emailEdit.text());
		settings.setSetting("checkForPackageUpdates", 
				String.valueOf(this.checkForPackageUpdates.isChecked()));
		settings.setSetting("checkForUpdates", 
				String.valueOf(this.checkForUpdates.isChecked()));
		settings.setSetting("askBeforeClosing",
				String.valueOf(this.askBeforeClosing.isChecked()));
		settings.setSetting("askBeforeWidgetDelete",
				String.valueOf(this.askBeforeWidgetDelete.isChecked()));
		settings.setSetting("snapToGrid", 
				String.valueOf(this.snapToGridCB.isChecked()));
		settings.setSetting("showSignalNames", 
				String.valueOf(this.showSignalNamesCB.isChecked()));
		settings.setSetting("iconSize", 
				String.valueOf(this.schemaIconSizeCombo.currentText()));
		settings.setSetting("toolbarIconSize", 
				String.valueOf(this.toolbarIconSizeCombo.currentText()));
		
		// TODO windowStyleSelect		= 	makeComboBox(lookFeelBox, "Window Style");
		/* TODO
		settings.setSetting("toolbarIconSize", 
				String.valueOf(this.toolbarIconSizeCombo.currentText()));
		
		language				=	makeComboBox(lookFeelBox, "Language");
		
		templatesDirList 		= 	makeListBox(underHood, "Templates Directory");
		*/
		
		settings.setSetting("outputVerbosity", 
				String.valueOf(this.verbosityCombo.currentIndex()));
		settings.setSetting("displayTraceback", 
				String.valueOf(this.displayTraceback.isChecked()));
		settings.setSetting("focusOnCatchException", 
				String.valueOf(this.focusOnCatchExceptionCB.isChecked()));
		settings.setSetting("uploadError", 
				String.valueOf(this.uploadError.isChecked()));
		settings.setSetting("askToUploadError", 
				String.valueOf(this.askToUploadError.isChecked()));
		settings.setSetting("writeLogFile", 
				String.valueOf(this.writeLogFileCB.isChecked()));
		// TODO this should display locations not url's
		settings.setSetting("CRANrepos", 
				String.valueOf(this.libListBox.currentItem()));
		
		
	}
	
	public void setMirror(QListWidgetItem old, QListWidgetItem n){
		
	}
	
	public void browseLogFile(){
		String fn = QFileDialog.getExistingDirectory(this, "Save Logs To", settings.getSetting("logsDir"));
		if (fn == null) return;
		// TODO just set the label and an internal variable, we should only set all of the settings when the user accepts
		
		// settings.setSetting("logsDir", fn);
	}
	
	public void addTemplateDirectory(){
		String dn = QFileDialog.getExistingDirectory(this, "Template Directory", "");
		if (dn == null) return;
		// TODO add the template file to the settings.  
		// if ()
	}
	
	public void removeTemplateDirectory(){
		StandardButton mb = QMessageBox.question(this, 
				"Remove Template Directory",
				"Are you sure you want to remove the selected template directory");
		if (mb.value() == QMessageBox.StandardButton.Ok.value()){
			// TODO get template dirs from settings and update, remove the selected directory
		}
	}
	
	private QCheckBox makeCheckBox(QWidget box, String label){
		QWidget parentBox = new QWidget(box);
		parentBox.setLayout(new QHBoxLayout());
		QLabel lab = new QLabel();
		parentBox.layout().addWidget(lab);
		lab.setText(label);
		QCheckBox cb = new QCheckBox(parentBox);
		parentBox.layout().addWidget(cb);
		
		return cb;
	}
	
	private QComboBox makeComboBox(QWidget box, String label){
		QWidget parentBox = new QWidget(box);
		parentBox.setLayout(new QHBoxLayout());
		QLabel lab = new QLabel();
		parentBox.layout().addWidget(lab);
		lab.setText(label);
		QComboBox cb = new QComboBox(parentBox);
		parentBox.layout().addWidget(cb);
		
		return cb;
	}
	
	private QListWidget makeListBox(QWidget box, String label){
		QWidget parentBox = new QWidget(box);
		parentBox.setLayout(new QHBoxLayout());
		QLabel lab = new QLabel();
		parentBox.layout().addWidget(lab);
		lab.setText(label);
		QListWidget cb = new QListWidget(parentBox);
		parentBox.layout().addWidget(cb);
		
		return cb;
	}
	
	public void onTabChanged(int i){
		
	}
}
